Skip to main content

文件 读写

//Documents 目录
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();

return directory.path;
}

//获取文件
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/counter.txt');
}

//写
Future<File> writeCounter(int counter) async {
final file = await _localFile;

// Write the file.
return file.writeAsString('$counter');
}

//读
Future<int> readCounter() async {
try {
final file = await _localFile;

// Read the file.
String contents = await file.readAsString();

return int.parse(contents);
} catch (e) {
// If encountering an error, return 0.
return 0;
}
}